<template>
  <div :class="className" :style="{height:height,width:width}" />
</template>

<script>
import echarts from 'echarts'
require('echarts/theme/macarons') // echarts theme
import resize from './mixins/resize'

export default {
  mixins: [resize],
  props: {
    className: {
      type: String,
      default: 'chart'
    },
    width: {
      type: String,
      default: '100%'
    },
    height: {
      type: String,
      default: '300px'
    },
    autoResize: {
      type: Boolean,
      default: true
    },
    chartData: {
      type: Object,
      required: true
    }
  },
  data() {
    return {
      chart: null
    }
  },
  watch: {
    chartData: {
      deep: true,
      handler(val) {
        this.setOptions(val)
      }
    }
  },
  mounted() {
    this.$nextTick(() => {
      this.initChart()
    })
  },
  beforeDestroy() {
    if (!this.chart) {
      return
    }
    this.chart.dispose()
    this.chart = null
  },
  methods: {
    initChart() {
      this.chart = echarts.init(this.$el, 'macarons')
      this.setOptions(this.chartData)
    },
    setOptions({ expectedData, keysdData } = {}) {
      this.chart.setOption({
        // backgroundColor: 'rgba(1,202,217,.2)',
        tooltip: {
          trigger: 'axis',
          axisPointer: {
            type: 'shadow'
          }
        },
        legend: {
          data: ['line', '日活跃数'],
          textStyle: {
            color: '#ccc'
          }
        },
        grid: {
          left: 20,
          right: 30,
          bottom: 20,
          top: 30,
          containLabel: true
        },
        xAxis: {
          data: keysdData,
          axisLine: {
            lineStyle: {
              color: '#ccc'
            }
          },
          boundaryGap: false
        },
        yAxis: {
          minInterval: 1,
          splitLine: { show: false },
          axisLine: {
            lineStyle: {
              color: '#ccc'
            }
          }
        },
        series: [
          //   {
          //   name: 'line',
          //   type: 'line',bar
          //   smooth: true,
          //   showAllSymbol: true,
          //   symbol: 'emptyCircle',
          //   symbolSize: 15,
          //   data: expectedData,
          //   color: new echarts.graphic.LinearGradient(0, 0, 0, 1, [{
          //       offset: 0,
          //       color: 'rgba(255, 191, 0)'
          //     }, {
          //       offset: 1,
          //       color: 'rgba(224, 62, 76)'
          //     }])
          // },
          {
            name: '日活跃数',
            type: 'line',
            barWidth: 10,
            itemStyle: {
              barBorderRadius: 5,
              color: new echarts.graphic.LinearGradient(0, 0, 0, 1, [{
                offset: 0,
                color: 'rgba(0, 221, 255)'
              }, {
                offset: 1,
                color: 'rgba(77, 119, 255)'
              }])
            },
            areaStyle: {},
            data: expectedData,
            animationDuration: 3800,
            animationEasing: 'cubicInOut'
          }]

      })
    }
  }
}
</script>
